<?php

/**
 * Registration class.
 * Registration is the data structure for inssert data into table users and profiles
 * Author: Khanh Nguyen
 * Date: 8-3-2014 4:17pm
 */
class mProfile extends User {

  public $id;
  public $id_profile;
  public $gender;
  public $seeking;
  public $colorHair;
  public $colorEye;
  public $tall;
  public $ethnicity;
  public $bodyType;
  public $marialStatus;
  public $childrenHave;
  public $childrenLiving;
  public $smoke;
  public $country;
  public $postalcode;
  public $city;
  public $state;
  public $dateOfBirth;
  public $userName;
  public $pass;
  public $email;
  public $greeting1;
  public $greeting2;
  public $greeting3;

  /**
   * Declares the validation rules.
   */
  public function rules() {
    return array(
        array('seeking, colorHair, colorEye, tall, ethnicity, bodyType, marialStatus, childrenHave, childrenLiving, smoke, country, dateOfBirth, userName, pass, email', 'required'),
        array('email', 'email'),
        //array('dateOfBirth', 'date', 'format'=>'yyyy-M-d'),
        array('smoke, postalcode, childrenHave, childrenLiving', 'numerical', 'integerOnly' => true),
        array('userName, pass', 'length', 'min' => 4, 'max' => 16),
        array('colorHair, colorEye, tall, ethnicity, marialStatus, smoke, country, postalcode, city, state', 'length', 'max' => 50)
    );
  }

  /**
   * Declares customized attribute labels.
   * If not declared here, an attribute would have a label that is
   * the same as its name with the first letter in upper case.
   */
  public function attributeLabels() {
    return array(
        'verifyCode' => 'Verification Code',
    );
  }

  /*
   * Alias Attribute Form into model so Validate
   */

  public function aliasAttrFormWithModel($data) {
    
  }

  public function getProfile($user_id){
    $user = Yii::app()->db->createCommand()
                  ->select('*')
                  ->from('users u')
                  ->join('profiles p', 'u.user_id=p.user_id')
                  ->where('u.user_id=:user_id', array(':user_id'=>$user_id))
                  ->queryRow();
    return $user;
  }
  
  public function getSamplePhotosInRegisterFree($seeking,$minage,$maxage,$country,$zipcode){
    $rs_country = Yii::app()->db->createCommand()
                  ->select('*')
                  ->from('profiles p')
                  ->where('p.country=:country', array(':country'=>$country))
                  ->queryRow();
    $rs_zipcode = Yii::app()->db->createCommand()
                  ->select('*')
                  ->from('profiles p')
                  ->where('p.postcode=:postcode', array(':postcode'=>$zipcode))
                  ->queryRow();
    if($rs_country==false && $rs_zipcode == false){
      $user = Yii::app()->db->createCommand()
                  ->select('p.*,u.username, up.user_id as user_idup, up.link_img')
                  ->from('profiles p')
                  ->join('users u', 'u.user_id=p.user_id')
                  ->leftjoin('user_photos up', 'up.approve = 1 and up.make_primary = 1 and up.user_id = p.user_id')
                  ->where('p.gender=:seeking and (YEAR(NOW())-YEAR(p.birthdate))>=:min and (YEAR(NOW())-YEAR(p.birthdate))<=:max', array(':seeking'=>$seeking,':min'=>$minage,':max'=>$maxage))
                  ->queryAll();
    }else if($rs_country!=false && $rs_zipcode == false){
      $user = Yii::app()->db->createCommand()
                  ->select('p.*,u.username, up.user_id as user_idup, up.link_img')
                  ->from('profiles p')
                  ->join('users u', 'u.user_id=p.user_id')
                  ->leftjoin('user_photos up', 'up.approve = 1 and up.make_primary = 1 and up.user_id = p.user_id')
                  ->where('p.gender=:seeking and p.country=:country and (YEAR(NOW())-YEAR(p.birthdate))>=:min and (YEAR(NOW())-YEAR(p.birthdate))<=:max', array(':country'=>$country, ':seeking'=>$seeking,':min'=>$minage,':max'=>$maxage))
                  ->queryAll();
    }else if($rs_country==false && $rs_zipcode != false){
      $user = Yii::app()->db->createCommand()
                  ->select('p.*,u.username, up.user_id as user_idup, up.link_img')
                  ->from('profiles p')
                  ->join('users u', 'u.user_id=p.user_id')
                  ->leftjoin('user_photos up', 'up.approve = 1 and up.make_primary = 1 and up.user_id = p.user_id')
                  ->where('p.gender=:seeking and p.postcode=:postcode and (YEAR(NOW())-YEAR(p.birthdate))>=:min and (YEAR(NOW())-YEAR(p.birthdate))<=:max', array(':postcode'=>$zipcode, ':seeking'=>$seeking,':min'=>$minage,':max'=>$maxage))
                  ->queryAll();
    }else if($rs_country!=false && $rs_zipcode != false){
       $user = Yii::app()->db->createCommand()
                  ->select('p.*,u.username, up.user_id as user_idup, up.link_img')
                  ->from('profiles p')
                  ->join('users u', 'u.user_id=p.user_id')
                  ->leftjoin('user_photos up', 'up.approve = 1 and up.make_primary = 1 and up.user_id = p.user_id')
                  //->where('p.seeking=:seeking and p.country=:country and p.postcode=:postcode and (YEAR(NOW())-YEAR(p.birthdate))>=:min and (YEAR(NOW())-YEAR(p.birthdate))<=:max', array(':country'=>$country,':postcode'=>$zipcode, ':seeking'=>$seeking,':min'=>$minage,':max'=>$maxage))
                  ->where('p.gender=:seeking and p.country=:country and (YEAR(NOW())-YEAR(p.birthdate))>=:min and (YEAR(NOW())-YEAR(p.birthdate))<=:max', array(':country'=>$country, ':seeking'=>$seeking,':min'=>$minage,':max'=>$maxage))
                  ->queryAll();
    }
    //var_dump($user);
    if(count($user)>0)
      return $user;
    else
      return false;
  }
  
  public function getProfileWithPhotosPrimary($user_id){
    $user = Yii::app()->db->createCommand()
                  ->select('p.*,u.*, u.user_id as user_idu, up.user_id as user_idup, up.link_img')
                  ->from('users u')
                  ->join('profiles p', 'u.user_id=p.user_id')
                  ->leftjoin('user_photos up', 'up.approve = 1 and up.make_primary = 1 and up.user_id = p.user_id')
                  ->where('u.user_id=:user_id', array(':user_id'=>$user_id))
                  ->queryRow();
    return $user;
  }
  /*
   * Description:; update profile
   * $key == name attribute in table
   * $responseValues: values to update to atrribute in table
   * $pofile_id: id_profile
   */
  public function updateProfile($key, $responseValues, $profile_id){
    $sql = "update profiles set $key = :responseValues where profile_id = :profile_id";
    $command=db()->createCommand($sql);
    $command->bindParam(":responseValues",$responseValues,PDO::PARAM_STR);
    $command->bindParam(":profile_id",$profile_id,PDO::PARAM_STR);
    $command->execute();
  }
  
  public function updateUser($key, $responseValues, $user_id){
    //var_dump(Yii::app()->session['profileUser']);
    $sql = "update users set $key = :responseValues where user_id = :user_id";
    $command=db()->createCommand($sql);
    $command->bindParam(":responseValues",$responseValues,PDO::PARAM_STR);
    $command->bindParam(":user_id",$user_id,PDO::PARAM_STR);
    $command->execute();
  }
  
  public function getProfileRecommend($sex,$country='',$postcode=''){
    $user = Yii::app()->db->createCommand()
                  ->select('p.*,u.*, u.user_id as user_idu, up.user_id as user_idup, up.link_img')
                  ->from('profiles p')
                  ->join('users u', 'u.user_id=p.user_id')
                  ->leftjoin('user_photos up', 'up.approve = 1 and up.make_primary = 1 and up.user_id = p.user_id')
                  ->where('p.gender=:sex', array(':sex'=>$sex))
                  ->limit(9)
                  ->order(array('RAND()'))
                  ->queryAll();
    if(count($user)>0)
      return $user;
    else
      return false;
  }
  
  public function getProfileByUserId($user_id){
    $user = Yii::app()->db->createCommand()
                  ->select('p.*,u.user_id as user_idu')
                  ->from('profiles p')
                  ->join('users u', 'u.user_id=p.user_id')
                  ->where('p.user_id=:user_id', array(':user_id'=>$user_id))
                  ->queryRow();
    if($user!=false)
      return $user;
    else
      return false;
  }
  
  public function delUserID($user_id){
    $command = Yii::app()->db->createCommand();
    $command->delete('profiles', 'user_id=:user_id', array(':user_id'=>$user_id));
  }
}